<?php
//ob_start();
include_once('settings.include.php');
include_once('includes/appcfg/connectBD.php');
    function ValidaUtilizador(){
        global $controller ;
        global $conn ;
        $usr = & $controller->getUser();
        $params['username']=
        $params['username']=$_GET['txtUtilizador'];   
        $params['password']=$_GET['txtPassword'];
        $credential = new SHA1BasicCredential();
        $validatorManager = new ValidatorManager() ;
        $validatorManager->register('username',new StringValidator());
        $validatorManager->register('password',new StringValidator());
        $validatorManager->execute($controller,$usr,$params);
        $validationErrors=$validatorManager->getExecutionErrors();
        if (!$validationErrors){
            $credential->setUsername($params['username']);
            $credential->setPassword($params['password']);
            $usr->setCredential($credential);
            $authH = new FAPDBSQLAuthenticationHandler(array());  
            $authH->setConnection($conn); 
                if ($authH->login($controller,$usr)){
                    $usr->setAuthenticated(true);
                    return true;
                }
            return false;
        }else{
            print_r($validationErrors);
        }
    }
    function ValidaUtilizadorLDAP($username,$pwd){
        global $controller ;
        global $conn ;
        
        $usr = & $controller->getUser();
        
        $params['username']= $username;
        $params['password']= $pwd;
        
        $ldapconfig['server'] = '135.177.22.91';
        $ldapconfig['port'] = 389;
        $ldapconfig['basedn'] = 'ou=PORTAL-USERS, ou=Testes, ou=Aplications, ou=ZENWorks, ou=RIALFA, o=FAP';    
        $credential = new BasicCredential();
        
        $validatorManager = new ValidatorManager() ;
        $validatorManager->register('username',new UsernameValidator());
        $validatorManager->register('password',new PasswordValidator());
        
        $validatorManager->execute($controller,$usr,$params);
        $validationErrors=$validatorManager->getExecutionErrors();
        
        if (!$validationErrors){
            $credential->setUsername($params['username']);
            $credential->setPassword($params['password']);
            $usr->setCredential($credential);
            $authH = new LDAPAuthenticationHandler($ldapconfig);  
            if ($authH->login($controller,$usr)){
                $usr->setAuthenticated(true);
                return true;
            }
            return false;
        }else{
            print_r($validationErrors);
        }
        
    }
    
    $bodyText = ValidaUtilizadorLDAP($_POST['txtUtilizador'],$_POST['txtPassword']  )?"OK":"NOK";

    $page = new Page("Pagina de validacao");
    $page->setBody($bodyText); 
    $page->execute();

?>